//
// C++ Implementation: mainactivitywidget
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "mainactivitywidget.h"



/**
 * Constructor.
 * @param db A reference to the database to use for all queries.
 * @param parent The parent widget.
 */
MainActivityWidget::MainActivityWidget ( QSqlDatabase* db, QWidget *parent )
        : QWidget ( parent )
{
    marbleWidget = new Marble::MarbleWidget;
    marbleWidget->setDownloadUrl ( "QUrl(http://download.kde.org/apps/marble/" );
    marbleWidget->setMapThemeId ( "earth/openstreetmap/openstreetmap.dgml" );
    // marbleWidget->setMapThemeId("earth/srtm/srtm.dgml");
    // marbleWidget->setMapThemeId("earth/bluemarble/bluemarble.dgml");


    //inputHandler = new Marble::MarbleWidgetTrainTrackerInputHandler;
    //marbleWidget->setInputHandler(inputHandler);

    //marbleWidget->setInputHandler(new Marble::MarbleWidgetDefaultInputHandler);

    setObjectName ( QString::fromUtf8 ( "Main Activity Widget" ) );

    interface = new ActivityDbInterface ( db );
    QDate plotStartDate(2008, 9, 1);
    QString filter("WHERE (type='Run' ");
    filter +=(" OR type='Workout') ");
    filter +=(" AND (date>'");
    filter += plotStartDate.toString(QString ("yyyyMMdd000000"));
    filter +=("') ");

    qDebug() << filter;
    interface-> setFilter ( filter );
    interface-> query();

    interface->getStatistics();

    plot = new ActivityPlot ( interface, "distance" );
    scene = new QGraphicsScene;
    scene->addItem ( plot );
    view = new QGraphicsView ( scene );
    view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    //enabling opengl can speedup plotting by an order of magnitude...
    //view->setViewport(new QGLWidget);

    vbl_main     = new QVBoxLayout;
    tw_bottom    = new QTabWidget;
    viewWidget   = new ActivityViewWidget ( interface );
    filterWidget = new ActivityFilterWidget ( interface );
    editWidget   = new ActivityEditWidget ( interface );


    // TODO: order these logically
    tw_bottom->addTab ( view,         "Plot" );
    tw_bottom->addTab ( filterWidget, "Filter" );
    tw_bottom->addTab ( editWidget,   "Add/Edit" );
    tw_bottom->addTab ( marbleWidget, "Map" );


    vbl_main->addWidget ( viewWidget );
    vbl_main->addWidget ( tw_bottom );

    setLayout ( vbl_main );
    show();

    editWidget->setDisabled ( true );

    // HACK:
    connect ( viewWidget, SIGNAL ( needEdit ( Activity ) ),
              editWidget, SLOT ( edit ( Activity ) ) );

    // edit causes reinterface->, reinterface-> causes viewWidget to update
    connect ( editWidget, SIGNAL ( updatedDb() ), interface, SLOT ( query() ) );
    connect ( interface, SIGNAL ( updatedModel ( ) ), viewWidget, SLOT ( updateUi () ) );
    connect ( interface, SIGNAL ( updatedModel ( ) ), plot, SLOT ( updateModel () ) );

    connect ( marbleWidget, SIGNAL ( mouseMoveGeoPosition ( QString ) ),
              this, SLOT ( handleOutput ( QString ) ) );

    connect (plot, SIGNAL(focused(int)), viewWidget, SLOT(showActivity(int)));

    //connect ( marbleWidget, SIGNAL(mouseClickGeoPosition (qreal, qreal, Marble::GeoDataCoordinates)),
    //          this, SLOT(mouseClick(qreal, qreal, Marble::GeoDataCoordinates)));
}


/**
* A test method.
* @param string The string to print out.
*/
void MainActivityWidget::handleOutput ( QString string )
{
    qDebug() << string;
}


/**
 * A test method to explore the features of the Marble widget.
 * @param lon The longitude of the point clicked.
 * @param lat The latitude of the point clicked.
 * @param  unit The units of the coordinates.
 */
void MainActivityWidget::mouseClick(qreal lon, qreal lat, Marble::GeoDataCoordinates::Unit)
{
    qDebug() << "lon: " << lon << " lat: " << lat;
}



